package cn.tedu.tea.service.impl;

import cn.tedu.tea.domain.Content;
import cn.tedu.tea.domain.ContentDetail;
import cn.tedu.tea.domain.dto.ContentDTO;
import cn.tedu.tea.domain.dto.ContentUpDTO;
import cn.tedu.tea.domain.vo.ContentVO;
import cn.tedu.tea.ex.ServiceException;
import cn.tedu.tea.mapper.ContentDetailMapper;
import cn.tedu.tea.mapper.ContentMapper;
import cn.tedu.tea.service.IContentService;
import cn.tedu.tea.web.ServiceCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author Hyanze
* @description 内容业务层
* @date 2023/4/21 2023/4/21
*/
@Service
public class ContentServiceImpl implements IContentService {

    @Autowired
    private ContentMapper contentMapper;

    @Autowired
    private ContentDetailMapper contentDetailMapper;

    @Override
    public List<ContentVO> list() {
        return contentMapper.list();
    }

    @Override
    public ContentVO getById(Integer id) {
        return contentMapper.getById(id);
    }

    @Override
    public void add(ContentDTO contentDTO) {
        Content content = new Content();
        BeanUtils.copyProperties(contentDTO,content);
        content.setEditor("markdown 富文本");
        content.setUp(0);
        content.setDown(0);
        content.setAuditStatus(1);
        content.setClickTimes(0);
        content.setCommentTimes(0);
        content.setGmtCreate(new Date());
        int contentAdd = contentMapper.add(content);
        if (contentAdd == 0) {
            throw new ServiceException(ServiceCode.ERR_CONFLICT);
        }
        ContentDetail contentDetail = new ContentDetail();
        contentDetail.setId(content.getId());
        contentDetail.setDetail(contentDTO.getDetail());
        contentDetail.setGmtModified(content.getGmtCreate());
        int contentDetailAdd = contentDetailMapper.add(contentDetail);
        if (contentDetailAdd == 0) {
            throw new ServiceException(ServiceCode.ERR_CONFLICT);
        }
    }

    @Override
    public void update(ContentUpDTO contentUpDTO) {
        Content content = new Content();
        BeanUtils.copyProperties(contentUpDTO,content);
        content.setGmtModified(new Date());
        content.setStatus(0);
        content.setAuditStatus(1);
        int i = contentMapper.update(content);
        if (i == 0) {
            throw new ServiceException(ServiceCode.ERR_CONFLICT);
        }
        ContentDetail contentDetail = new ContentDetail();
        contentDetail.setId(contentUpDTO.getId());
        contentDetail.setDetail(contentUpDTO.getDetail());
        contentDetail.setGmtModified(new Date());
        int i1 = contentDetailMapper.update(contentDetail);
        if (i1 == 0) {
            throw new ServiceException(ServiceCode.ERR_CONFLICT);
        }
    }

    @Override
    public void deleteById(Integer id) {
        int i = contentMapper.deleteById(id);
        if (i == 0) {
            throw new ServiceException(ServiceCode.ERR_CONFLICT);
        }
        int i1 = contentDetailMapper.deleteById(id);
        if (i1 == 0) {
            throw new ServiceException(ServiceCode.ERR_CONFLICT);
        }
    }
}
